/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ProjectMgtSystem.Bean;

import ProjectMgtSystem.Entities.Engineer;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import java.util.*;
import java.sql.*;
import ProjectMgtSystem.Utilities.*;
import ProjectMgtSystem.Entities.Project;
import ProjectMgtSystem.Entities.Task;
/**

 *
 * @author Su Duy Hien
 * Create 2011/07/19
 * Last Updated: 2011/07/21

 * ActionBean class of project page


 */
public class ProjectActionBean extends org.apache.struts.action.ActionForm {
    // <editor-fold defaultstate="collapsed" desc="declare Parameter">
    private String projectID;
    private String projectName;
    private String teamLeader;
    private String description;
    private String planStartDate;
    private String planEndDate;
    private String actualStartDate;
    private String actualEndDate;    
    private String completeRatio;
    private String language;
    
//  taskID, taskName used for assign task to project    
    private ArrayList<Task> assignedTaskList;
    private ArrayList<Task> notAssignedTaskList;
    private String[] selectedItems = {};
    private String taskID; 
    
    // </editor-fold>
    
    // <editor-fold defaultstate="collapsed" desc="Get and Set">
    /**
     *
     */
    public ProjectActionBean() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param request The HTTP Request we are processing.
     * @return
     */
    @Override
    public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
        ActionErrors errors = new ActionErrors();        
        return errors;
    }

    /**
     * @return the projectID
     */
    public String getProjectID() {
        return projectID;
    }

    /**
     * @param projectID the projectID to set
     */
    public void setProjectID(String projectID) {
        this.projectID = projectID;
    }

    /**
     * @return the projectName
     */
    public String getProjectName() {
        return projectName;
    }

    /**
     * @param projectName the projectName to set
     */
    public void setProjectName(String projectName) {
        this.projectName = projectName;
    }

    /**
     * @return the teamLeader
     */
    public String getTeamLeader() {
        return teamLeader;
    }

    /**
     * @param teamLeader the teamLeader to set
     */
    public void setTeamLeader(String teamLeader) {
        this.teamLeader = teamLeader;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @return the planStartDate
     */
    public String getPlanStartDate() {
        return planStartDate;
    }

    /**
     * @param planStartDate the planStartDate to set
     */
    public void setPlanStartDate(String planStartDate) {
        this.planStartDate = planStartDate;
    }

    /**
     * @return the planEndDate
     */
    public String getPlanEndDate() {
        return planEndDate;
    }

    /**
     * @param planEndDate the planEndDate to set
     */
    public void setPlanEndDate(String planEndDate) {
        this.planEndDate = planEndDate;
    }

    /**
     * @return the actualStartDate
     */
    public String getActualStartDate() {
        return actualStartDate;
    }

    /**
     * @param actualStartDate the actualStartDate to set
     */
    public void setActualStartDate(String actualStartDate) {
        this.actualStartDate = actualStartDate;
    }

    /**
     * @return the actualEndDate
     */
    public String getActualEndDate() {
        return actualEndDate;
    }

    /**
     * @param actualEndDate the actualEndDate to set
     */
    public void setActualEndDate(String actualEndDate) {
        this.actualEndDate = actualEndDate;
    }

    /**
     * @return the completeRatio
     */
    public String getCompleteRatio() {
        return completeRatio;
    }

    /**
     * @param completeRatio the completeRatio to set
     */
    public void setCompleteRatio(String completeRatio) {
        this.completeRatio = completeRatio;
    }

    /**
     * @return the language
     */
    public String getLanguage() {
        return language;
    }

    /**
     * @param language the language to set
     */
    public void setLanguage(String language) {
        this.language = language;
    }

    public ArrayList<Task> getAssignedTaskList() {
        return assignedTaskList;
    }

    public void setAssignedTaskList(ArrayList<Task> assignedTaskList) {
        this.assignedTaskList = assignedTaskList;
    }

    public ArrayList<Task> getNotAssignedTaskList() {
        return notAssignedTaskList;
    }

    public void setNotAssignedTaskList(ArrayList<Task> notAssignedTaskList) {
        this.notAssignedTaskList = notAssignedTaskList;
    }

    public String[] getSelectedItems() {
        return selectedItems;
    }

    public void setSelectedItems(String[] selectedItems) {
        this.selectedItems = selectedItems;
    }

    public String getTaskID() {
        return taskID;
    }

    public void setTaskID(String taskID) {
        this.taskID = taskID;
    }
    
    
    // </editor-fold>
    
// <editor-fold defaultstate="collapsed" desc="View, Update, Insert, Delete, Select methods by HIENSD 25/07/2011">
    /**
     * get arraylist of project
     */
    public ArrayList<Project> view() throws SQLException {
        ArrayList<Project> PList = new ArrayList<Project>();                
        DBHelper.getInstance().initConnection();
        ResultSet rs = DBHelper.getInstance().executeQueryProc("View_Project",null);
        
        while (rs.next()) {
            Project project = new Project();            
            project.setProjectID(rs.getString("ProjectID"));
            project.setProjectName(rs.getString("ProjectName"));
            
            Engineer en = new Engineer();
            en.setEngineerID(rs.getString("TeamLeaderID"));
            en.setFirstName(rs.getString("FirstName"));
            en.setLastName(rs.getString("LastName"));
            project.setTeamLeader(en);            
            
            project.setDescription(rs.getString("Description"));
            project.setPlanStartDate(rs.getString("PlanStartDate"));
            project.setPlanEndDate(rs.getString("PlanEndDate"));
            PList.add(project);
        }
        DBHelper.getInstance().closeConnection();
        return PList;
    }
    
    /**
     * insert a new project record
     */
    public String insert(Project p) throws SQLException {        
        DBHelper.getInstance().initConnection();
        ArrayList<String> arrP= new ArrayList<String>();
        arrP.add(p.getProjectID());
        arrP.add(p.getProjectID());
        arrP.add(p.getTeamLeader().getEngineerID());        
        arrP.add(p.getDescription());
        arrP.add(p.getPlanStartDate());
        arrP.add(p.getPlanEndDate());
        String message = DBHelper.getInstance().executeUpdateProcReturn("Insert_Project", arrP);
        DBHelper.getInstance().closeConnection();
        return message;
    }
    
     /**
     * Delete
     */
    public String delete(String projectID) throws SQLException {
        DBHelper.getInstance().initConnection();
        ArrayList<String> arrP= new ArrayList<String>();
        arrP.add(projectID);
        String message = DBHelper.getInstance().executeUpdateProcReturn("Delete_Project", arrP);
        DBHelper.getInstance().closeConnection();
        return message;
    }
    
     /**
     * Select with @param projectID
     */
    public ArrayList<Project> select(String projectID) throws SQLException {
        ArrayList<Project> pList = new ArrayList<Project>();        
        ArrayList<String> arrP= new ArrayList<String>();
        arrP.add(projectID);
        
        DBHelper.getInstance().initConnection();
        ResultSet rs = DBHelper.getInstance().executeQueryProc("View_Project_ByID",arrP);
        
        while (rs.next()) {
            Project project = new Project();
            project.setProjectID(rs.getString("ProjectID"));
            project.setProjectName(rs.getString("ProjectName"));
            
            Engineer en = new Engineer();
            en.setEngineerID(rs.getString("TeamLeaderID"));
            en.setFirstName(rs.getString("FirstName"));
            en.setLastName(rs.getString("LastName"));
            project.setTeamLeader(en);            
            
            project.setDescription(rs.getString("Description"));
            project.setPlanStartDate(rs.getString("planStartDate"));
            project.setPlanEndDate(rs.getString("planEndDate"));            
            pList.add(project);
        }
        DBHelper.getInstance().closeConnection();
        return pList;
    }
      /**
     * Update
     */
    public String update(Project p) throws SQLException {
        DBHelper.getInstance().initConnection();
        ArrayList<String> arrP= new ArrayList<String>();
        arrP.add(p.getProjectID());
        arrP.add(p.getProjectID());
        arrP.add(p.getTeamLeader().getEngineerID());        
        arrP.add(p.getDescription());
        arrP.add(p.getPlanStartDate());
        arrP.add(p.getPlanEndDate());
        String message = DBHelper.getInstance().executeUpdateProcReturn("Update_Project_Admin", arrP);
        DBHelper.getInstance().closeConnection();
        return message;
    }
    // </editor-fold>
    
// <editor-fold defaultstate="collapsed" desc="viewTasksAssigned   by ThinhDD CreateDate: 2011/07/25">    
    public ArrayList<Task> viewTasksAssigned(String projectID) throws SQLException {

        DBHelper.getInstance().initConnection();
        ArrayList<String> arrS = new ArrayList<String>();
        arrS.add(projectID);
        ResultSet rs = DBHelper.getInstance().executeQueryProc("View_TaskAssigned", arrS);

        ArrayList<Task> taskList = new ArrayList<Task>();


        while (rs.next()) {
            Task task = new Task();
            task.setTaskID(rs.getString("taskID"));
            task.setTaskName(rs.getString("taskName"));

            taskList.add(task);
        }

        DBHelper.getInstance().closeConnection();
        return taskList;
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="viewTasksNotAssigned   by ThinhDD CreateDate: 2011/07/25">    
    public ArrayList<Task> viewTasksNotAssigned(String projectID) throws SQLException {

        DBHelper.getInstance().initConnection();
        ArrayList<String> arrS = new ArrayList<String>();
        arrS.add(projectID);
        ResultSet rs = DBHelper.getInstance().executeQueryProc("View_TaskNotAssigned", arrS);

        ArrayList<Task> taskList = new ArrayList<Task>();

        while (rs.next()) {
            Task task = new Task();
            task.setTaskID(rs.getString("taskID"));
            task.setTaskName(rs.getString("taskName"));

            taskList.add(task);
        }

        DBHelper.getInstance().closeConnection();
        return taskList;
    }
// </editor-fold>    

// <editor-fold defaultstate="collapsed" desc="assignTasks   by ThinhDD CreateDate: 2011/07/25">  
    public String assignTasks(String projectID, String taskID) throws SQLException {

        DBHelper.getInstance().initConnection();
        ArrayList<String> arrS = new ArrayList<String>();
        arrS.add(projectID);
        arrS.add(taskID);
        String message = DBHelper.getInstance().executeUpdateProcReturn("Insert_TaskAssigned", arrS);

        DBHelper.getInstance().closeConnection();
        return message;
    }
//</editor-fold>
    
// <editor-fold defaultstate="collapsed" desc="deleteTasks   by ThinhDD CreateDate: 2011/07/25">  
    public String deleteTask(String projectID, String taskID) throws SQLException {

        DBHelper.getInstance().initConnection();
        ArrayList<String> arrS = new ArrayList<String>();
        arrS.add(projectID);
        arrS.add(taskID);
        String message = DBHelper.getInstance().executeUpdateProcReturn("Delete_TaskAssigned", arrS);

        DBHelper.getInstance().closeConnection();
        return message;
    }
//</editor-fold>    
}
